package com.itheima.web.controller.ums;

import com.itheima.service.ums.ResourceService;
import com.itheima.utils.ResultData;
import com.itheima.vo.pms.Brand;
import com.itheima.vo.ums.UmsResource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("resource")
public class ResourceController {

    @DubboReference
    private ResourceService resourceService;

    /**
     * 添加后台资源
     * @param resource
     * @return
     */
    @PostMapping("/create")
    public ResultData create (@RequestBody UmsResource resource){
        log.info("resource:{}", resource);
        return resourceService.insert(resource);
    }


    /**
     * 根据ID删除后台资源
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResultData delete(@PathVariable  Integer id ){
        log.info("deleteId:{}",id);
        resourceService.removeById(id);
        return ResultData.ok("删除成功","ok");
    }

    /**
     * 分页模糊查询后台资源
     * @param categoryId
     * @param nameKeyword
     * @param pageNum
     * @param pageSize
     * @param urlKeyword
     * @return
     */
    @GetMapping("/list")
    public ResultData list (Integer categoryId,String nameKeyword,Integer pageNum,Integer pageSize ,String urlKeyword){
        return resourceService.listPage(categoryId,nameKeyword, pageNum ,pageSize , urlKeyword);
    }

    /*
    查询所有后台资源
    */
    @GetMapping("/listAll")
    public ResultData listAll ( ){
        List<UmsResource> list = resourceService.list();
        return ResultData.ok("OK" , list);
    }


    /**
     *修改后台资源
     * @param id
     * @param resource
     * @return
     */
    @PostMapping("update/{id}")
    public ResultData updateById(@PathVariable Integer id, @RequestBody UmsResource resource){
        resource.setId(id);
        resourceService.updateById(resource);
        return ResultData.ok("操作成功","");
    }


    /**
     *根据ID获取资源详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResultData getById(@PathVariable Integer id){
        return ResultData.ok("操作成功",resourceService.getById(id));
    }
}
